package com.fliad.ai.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.fliad.ai.entity.AiLlm;
import com.fliad.ai.entity.AiMcp;
import com.fliad.ai.entity.AiWorkflow;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.noear.solon.flow.Chain;
import org.noear.solon.flow.stateful.StatefulFlowEngine;
import org.noear.wood.DbContext;
import org.noear.wood.annotation.Db;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

@Component
public class AiWorkflowService {

    @Db
    DbContext db;

    public boolean create(AiWorkflow workflow) throws SQLException {
        return insertWorkflow(workflow);
    }

    // 新增方法：插入工作流
    private boolean insertWorkflow(AiWorkflow workflow) throws SQLException {
        return db.table("ai_workflow")
                .setEntity(workflow)
                .insert() > 0;
    }

    public AiWorkflow get(Long id) throws SQLException {
        return db.table("ai_workflow")
                .whereEq("id", id)
                .selectItem("*", AiWorkflow.class);
    }

    public List<AiWorkflow> list() throws SQLException {
        return db.table("ai_workflow")
                .orderBy("id DESC")
                .selectList("*", AiWorkflow.class);
    }

    public boolean update(Long id, AiWorkflow workflow) throws SQLException {
        workflow.setId(id);
        return db.table("ai_workflow")
                .whereEq("id", id)
                .setEntity(workflow)
                .update() > 0;
    }

    public boolean saveFlow(Long id, AiWorkflow workflow) throws SQLException {
        return db.table("ai_workflow")
                .whereEq("id", id)
                .setEntity(workflow)
                .update() > 0;
    }

    public boolean delete(Long id) throws SQLException {
        return db.table("ai_workflow")
                .whereEq("id", id)
                .delete() > 0;
    }

}
